package com.ulife.permission.module.screen;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.ulife.common.ErrorMessage;
import com.ulife.common.Result;
import com.ulife.common.exception.ManagerException;
import com.ulife.permission.common.RoleQueryResult;
import com.ulife.permission.common.UserQueryResult;
import com.ulife.permission.dao.dataobject.RoleDO;
import com.ulife.permission.dao.dataobject.UserDO;
import com.ulife.permission.manager.RoleManager;
import com.ulife.permission.manager.UserManager;
import com.ulife.permission.module.vo.UserVO;

public class UserInfo {

	private static final Logger log = LoggerFactory.getLogger(UserInfo.class);

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private UserManager userManager;
	
	@Autowired
	private RoleManager roleManager;
	
	public void execute(Context context, Navigator nav) {
		String userIdStr = request.getParameter("userId");
		long userId = 0;
		if (StringUtils.isNotBlank(userIdStr) && StringUtils.isNumeric(userIdStr)) {
			userId = Long.parseLong(userIdStr);
			try {
				Result<UserDO> result = userManager.getUserById(userId);
				if (result != null && result.isSuccess() && result.getModule() != null) {
					UserDO user = result.getModule();
					if (user.getStatus() != 0) {
						context.put("error", "该用户不可用");
						nav.forwardTo("error");
					}
					UserVO userVO = new UserVO();
					BeanUtils.copyProperties(user, userVO);
					String roleIdStr = user.getRoleIdList();
					if (StringUtils.equals(roleIdStr, "admin")) {
						context.put("isAdmin", true);
					}
					Map<String, Long> roleMap = buildMap(roleIdStr);
					context.put("roleMap", roleMap);
					context.put("user", userVO);
					
					UserQueryResult userResult = userManager.getAllAvailableUsers();
					List<UserDO> userList = null;
					if (CollectionUtils.isNotEmpty(userResult.getUserList())){
						userList = userResult.getUserList();
					}
					context.put("users", userList);
					
					RoleQueryResult roleResult = roleManager.getAllAvailableRoles();
					List<RoleDO> roleList = null;
					if (CollectionUtils.isNotEmpty(roleResult.getRoleList())){
						roleList = roleResult.getRoleList();
					}
					context.put("roles", roleList);
				}
				else {
					context.put("error", ErrorMessage.USER_INFO_ERROR);
					nav.forwardTo("error");
				}
				context.put("module", "user");
			} catch (ManagerException e) {
				context.put("error", ErrorMessage.USER_INFO_ERROR);
				nav.forwardTo("error");
				log.error("get user info error, user id = " + userIdStr, e);
			}
		}
		else {
			context.put("error", ErrorMessage.USER_INFO_ERROR);
			nav.forwardTo("error");
		}
	}

	private Map<String, Long> buildMap(String list) {
		Map<String, Long> map = new HashMap<String, Long>();
		if (StringUtils.isNotBlank(list)) {
			String[] strings = StringUtils.split(list, ",");
			for (String s : strings) {
				map.put(s, 1L);
			}
		}
		return map;
	}
	
}
